package com.yuanfeng.biz.admin.type.web;

import com.yuanfeng.entity.Types;
import com.yuanfeng.util.BaseServlet;
import com.yuanfeng.biz.admin.type.service.TypeService;
import com.yuanfeng.biz.admin.type.service.TypeServiceImpl;
import com.yuanfeng.util.WebUtil;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TypeServlet extends BaseServlet {
    TypeService service = new TypeServiceImpl();

    public void queryTypes(HttpServletRequest req, HttpServletResponse resp)
            throws Exception {
        // 查询出所有图书分类，然后放到request中去
        List<Types> types = service.queryTypes();
        req.setAttribute("types", types);
        req.getRequestDispatcher("/jsp/admin/type/list.jsp").forward(req, resp);
    }

    /**
     * 保存方法
     */
    public void save(HttpServletRequest req, HttpServletResponse resp)
            throws Exception {
        Types types = WebUtil.copyRequestToBean(req, Types.class);
        types = service.add(types);
        req.setAttribute("type", types);
        req.getRequestDispatcher("/jsp/admin/type/addback.jsp").forward(req, resp);
    }

    /**
     * 删除分类
     */
    public void deleteTypes(HttpServletRequest req, HttpServletResponse resp)
            throws Exception {
        Types types = WebUtil.copyRequestToBean(req, Types.class);
        service.delete(types);
        this.queryTypes(req, resp);

    }

}
